# usarsim.py
# Contains the interface code used by Blender objects
# to communicate using the UsarSim interface

import GameLogic;
import usarcore;
import usarrobot;

def processCore():
    try:
        core = GameLogic.core;
    except AttributeError, e:
        core = usarcore.Core();
        GameLogic.core = core;
    return core.process();

def processRobot(name, obj):
    try:
        robotObjs = GameLogic.robotObjs;
    except AttributeError, e:
        robotObjs = {};
        GameLogic.robotObjs = robotObjs;
    robotObjs[obj] = name;

    try:
        robots = GameLogic.robots;
    except AttributeError, e:
        robots = {};
        GameLogic.robots = robots;

    try:
        robot = robots[name];
    except KeyError, e:
        robot = usarrobot.Robot();
        robots[name] = robot;

    return robot.process();

def getRobot(gameObject):
    try:
        robotObjs = GameLogic.robotObjs;
    except:
        return None;

    robot = robotObjs.get(gameObject);
    if robot:
        return robot;
    else:
        parent = gameObject.getParent();
        if parent:
            return getRobot(parent);
        else:
            return None;
